package com.alquiler.controllers;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.animation.FadeTransition;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.stage.Screen;
import javafx.util.Duration;

import com.alquiler.entities.Empleado;
import com.alquiler.implementation.IScreenParent;
import com.alquiler.types.ROL;
import com.alquiler.utils.ConstantesVistas;

public class LayoutController implements Initializable, IScreenParent{

	MainController mainController;	
	
	@FXML
	private BorderPane layout;
	
    @FXML
    private MenuItem empleadoMenu;

    @FXML
    private MenuItem usuariosMenu;   

    @FXML
    private MenuItem sedesMenu;	
    
    @FXML
    private MenuItem canchasMenu;
    
    @FXML
    private MenuItem informeAlquilerMenu;

    @FXML
    private MenuItem InformeCanchaMenu;
    
    
	
	@FXML
    private MenuItem exitMenu;	
	
	@FXML
	private Pane content;
	
	
	@FXML
	private void exitStage(ActionEvent event){			
		mainController.getModalConfirmacion(ConstantesVistas.screenModalConfirmacion);
	}
	
	@FXML
    void getViewEmpleado(ActionEvent event) {
		this.setCenterScreen(ConstantesVistas.screenEmpleado);
    }

    @FXML
    void getViewUsuario(ActionEvent event) {

    }

    @FXML
    void getViewSedes(ActionEvent event) {
    	this.setCenterScreen(ConstantesVistas.screenSede);
    }

    @FXML
    void getViewCanchas(ActionEvent event) {
    	this.setCenterScreen(ConstantesVistas.screenCancha);

    }
    
    //Reservas
    @FXML
    void getViewReserva(ActionEvent event) {
    	
    	this.setCenterScreen(ConstantesVistas.screenReserva);

    }
   

    @FXML
    void getViewInformeCancha(ActionEvent event) {
    	this.setCenterScreen(ConstantesVistas.screenReporteCancha);

    }

    @FXML
    void getViewinformeAlquiler(ActionEvent event) {

    }

	
	
	
	@Override
	public void initialize(URL url, ResourceBundle rb) {
		
		this.setCenterScreen(ConstantesVistas.screenIndex);
		
		this.layout.setMinHeight((Screen.getPrimary().getBounds().getHeight()/10) * 8.4);
		this.layout.setMinWidth((Screen.getPrimary().getBounds().getWidth()/10) * 9.5);
	    
	}
	
	
	public void setScreenParent(MainController screenParent){
		mainController = screenParent;
		
		Empleado empleado = mainController.getEmpleado();
		
		if(empleado != null){
			
			if(empleado.getRol().equals(ROL.EMPLEADO)){
				
				empleadoMenu.setVisible(false);
				usuariosMenu.setVisible(false);			
								
			}			
		}
		
    }	
	
	
	
	public void setCenterScreen(final String nameScreen){	
		
		
		
		if(this.content.getChildren().size()>0){
			
		    FadeTransition fadeTransitionOut = new FadeTransition(Duration.millis(800), content);
		    fadeTransitionOut.setFromValue(1.0);
		    fadeTransitionOut.setToValue(0.0);
		    fadeTransitionOut.play();    
		    
		    fadeTransitionOut.setOnFinished(new EventHandler<ActionEvent>() {				
				@Override
				public void handle(ActionEvent event) {
					content.getChildren().remove(0);
					content.getChildren().add(0,mainController.loadVista(nameScreen));					
					
					FadeTransition fadeTransitionIn = new FadeTransition(Duration.millis(1000), content);
					fadeTransitionIn.setFromValue(0.0);
					fadeTransitionIn.setToValue(1.0);
					fadeTransitionIn.play();
				}
			});					
			
		}else{
			MainController mainC = new MainController();
			this.content.getChildren().add(mainC.loadVista(nameScreen));		
		}
	}
	

}
